@page "/"
@using System.Globalization;
@using Microsoft.AspNetCore.WebUtilities;
@inject NavigationManager UriHelper
@inject NavigationManager NavigationManager

<form>
    <fieldset>
        <label for="locale">Language</label>
        <select id="locale" name="locale" @onchange="LocaleChanged" value="@locale">
            <option value="en-US">English</option>
            <option value="zh-CN">Chinese (Simplified)</option>
            <option value="zh-TW">Chinese (Traditional)</option>
            <option value="da">Danish</option>
            <option value="nl">Dutch</option>
            <option value="fi">Finnish</option>
            <option value="fr">French</option>
            <option value="de">German</option>
            <option value="hu">Hungarian</option>
            <option value="it">Italian</option>
            <option value="ja">Japanese</option>
            <option value="nb">Norwegian</option>
            <option value="pl">Polish</option>
            <option value="pt">Portuguese</option>
            <option value="ro">Romanian</option>
            <option value="ru">Russian</option>
            <option value="sl-SI">Slovenian</option>
            <option value="es">Spanish</option>
            <option value="sv">Swedish</option>
            <option value="tr">Turkish</option>
            <option value="uk">Ukrainian</option>
        </select>
        <label for="expression">Cron Expression</label>
        <input id="expression" name="expression" @bind="expression" placeholder="" type="text" />
        <button type="button" class="button button-black" id="describe" @onclick="UpdateDescription">Describe</button>
    </fieldset>
</form>
<h3>Output</h3>
<blockquote>
    <div id="description" class="description">
        @description
    </div>
</blockquote>

@code {
    private string locale = "en-US";
    private string expression = "0 23 ? * MON-FRI";
    private string description = "";
    private void UpdateDescription()
    {
        var options = new CronExpressionDescriptor.Options()
        {
            ThrowExceptionOnParseError = false,
            Verbose = false,
            DayOfWeekStartIndexZero = true,
        };

        this.description = CronExpressionDescriptor.ExpressionDescriptor.GetDescription(this.expression, options);
    }

    private void LocaleChanged(ChangeEventArgs e)
    {
        NavigationManager.NavigateTo($"?locale={e.Value.ToString()}&expression={this.expression}", true);
    }

    protected override void OnInitialized()
    {
        var uri = UriHelper.ToAbsoluteUri(UriHelper.Uri);
        this.expression = QueryHelpers.ParseQuery(uri.Query).TryGetValue("expression", out var type) ? type.First() : this.expression;

        this.locale = CultureInfo.DefaultThreadCurrentCulture?.ToString() ?? this.locale;

        this.UpdateDescription();
    }
}
